/**
 * 路由工具函数
 * 简化 uniapp 路由跳转操作
 */

// 路由映射表 - 可以自定义简短的路由名称
const routeMap = {
  // ========== 首页相关 ==========
  'home': '/pages/index/index',
  'index': '/pages/index/index',
  'search': '/pages/index/search',
  
  // ========== 任务相关 ==========
  'tasks': '/pages/tasks/index',
  'task-detail': '/pages/tasks/detail',
  'task-article': '/pages/tasks/article',
  'task-orders': '/pages/tasks/orderlist',
  'task-billorder': '/pages/tasks/billorder',
  'task-rightsorder': '/pages/tasks/rightsorder',
  'task-consignment': '/pages/tasks/consignment',
  'task-addkey': '/pages/tasks/addkey',
  
  // ========== 用户相关 ==========
  'profile': '/pages/users/profile',
  'user-settings': '/pages/users/settings',
  'user-nickname': '/pages/users/nickname',
  'user-phone': '/pages/users/phone',
  
  // ========== 登录相关 ==========
  'login': '/pages/login/login',
  'wx-login': '/pages/login/wxlogin',
  'register': '/pages/login/register',
  'logout': '/pages/login/logout',
  
  // ========== 商品相关 ==========
  'goods-detail': '/pages/goods/goodsdetail',
  'goods-category': '/pages/goods/goodscategory',
  'goods-list': '/pages/goods/goodslist',
  
  // ========== 订单相关 ==========
  'order-list': '/pages/order/orderlist',
  'order-buy-detail': '/pages/order/buydetail',
  'order-card-manage': '/pages/order/cardmanage',
  'order-billorder-detail': '/pages/order/billorderdetail',
  
  // ========== 商店相关 ==========
  'shop-card-manage': '/pages/shop/cardmanage',
  'shop-order-detail': '/pages/shop/orderdetail',
  'shop-order-list': '/pages/shop/orderlist',
  
  // ========== 钱包相关 ==========
  'wallet-commission': '/pages/wallet/commission',
  'wallet-account': '/pages/wallet/account',
  'wallet-balance': '/pages/wallet/balance',
  'wallet-withdraw': '/pages/wallet/withdraw',
  'wallet-success': '/pages/wallet/success',
  'wallet-recharge': '/pages/wallet/recharge',
  
  // ========== 团队相关 ==========
  'team': '/pages/team/index',
  'team-list': '/pages/team/teamlist',
  
  // ========== 分销相关 ==========
  'fenxiao': '/pages/fenxiao/index',
  'fenxiao-orders': '/pages/fenxiao/orderlist',
  'fenxiao-order-detail': '/pages/fenxiao/orderdetail',
  
  // ========== 公告消息相关 ==========
  'notice-text-list': '/pages/notice/textlist',
  'notice-list': '/pages/notice/noticelist',
  'notice-text-detail': '/pages/notice/textdetail',
  'notice-guide-list': '/pages/notice/guidelist',
  'notice-message': '/pages/notice/message',
  'notice-trade-list': '/pages/notice/tradelist',
  
  // ========== 实名认证相关 ==========
  'realname': '/pages/realname/index',
  'realname-certified': '/pages/realname/certified',
  
  // ========== 支付相关 ==========
  'pay-success': '/pages/pay/success',
  
  // ========== 通用页面 ==========
  'common-success': '/pages/common/success',
  
  // ========== 测试页面 ==========
  'router-test': '/pages/test/router-test',
  
  // ========== 简化映射（常用页面的超短别名） ==========
  '/': '/pages/index/index',
  '/home': '/pages/index/index',
  '/tasks': '/pages/tasks/index',
  '/profile': '/pages/users/profile',
  '/login': '/pages/login/login',
  '/wallet': '/pages/wallet/account',
  '/team': '/pages/team/index',
  '/fenxiao': '/pages/fenxiao/index',
  '/search': '/pages/index/search',
  '/orders': '/pages/order/orderlist',
  '/shop': '/pages/shop/orderlist',
  '/notice': '/pages/notice/message',
  '/realname': '/pages/realname/index'
}

/**
 * 获取完整路由路径
 * @param {string} route - 路由名称或完整路径
 * @returns {string} 完整的路由路径
 */
function getFullPath(route) {
  // 如果已经是完整路径，直接返回
  if (route.startsWith('/pages/')) {
    return route
  }
  
  // 如果是映射表中的简短名称，返回对应的完整路径
  if (routeMap[route]) {
    return routeMap[route]
  }
  
  // 如果不是以 / 开头，假设是相对于 pages 的路径
  if (!route.startsWith('/')) {
    return `/pages/${route}`
  }
  
  return route
}

/**
 * 页面跳转 - 保留当前页面
 * @param {string|object} options - 路由名称或跳转选项
 */
export function navigateTo(options) {
  if (typeof options === 'string') {
    options = { url: options }
  }
  
  const url = getFullPath(options.url)
  
  return uni.navigateTo({
    ...options,
    url
  })
}

/**
 * 页面重定向 - 关闭当前页面
 * @param {string|object} options - 路由名称或跳转选项
 */
export function redirectTo(options) {
  if (typeof options === 'string') {
    options = { url: options }
  }
  
  const url = getFullPath(options.url)
  
  return uni.redirectTo({
    ...options,
    url
  })
}

/**
 * Tab 页面切换
 * @param {string|object} options - 路由名称或跳转选项
 */
export function switchTab(options) {
  if (typeof options === 'string') {
    options = { url: options }
  }
  
  const url = getFullPath(options.url)
  
  return uni.switchTab({
    ...options,
    url
  })
}

/**
 * 重新启动应用
 * @param {string|object} options - 路由名称或跳转选项
 */
export function reLaunch(options) {
  if (typeof options === 'string') {
    options = { url: options }
  }
  
  const url = getFullPath(options.url)
  
  return uni.reLaunch({
    ...options,
    url
  })
}

/**
 * 返回上一页
 * @param {number} delta - 返回的页面数
 */
export function navigateBack(delta = 1) {
  return uni.navigateBack({
    delta
  })
}

/**
 * 预加载页面
 * @param {string|object} options - 路由名称或预加载选项
 */
export function preloadPage(options) {
  if (typeof options === 'string') {
    options = { url: options }
  }
  
  const url = getFullPath(options.url)
  
  return uni.preloadPage({
    ...options,
    url
  })
}

// 导出路由映射表，方便外部使用
export { routeMap }

// 默认导出
export default {
  navigateTo,
  redirectTo,
  switchTab,
  reLaunch,
  navigateBack,
  preloadPage,
  routeMap,
  getFullPath
}